package br.com.uglyboys.service;

import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.util.Hashtable;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import br.com.uglyboys.control.CorreioController;
import br.com.uglyboys.model.ObjetoRastreavel;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class BatTrackingService extends Service {

	private Timer timer = new Timer();
	private static final long UPDATE_INTERVAL = 5000;

	@Override
	public void onCreate() {
	  super.onCreate();

	  // init the service here
	  _startService();

	}

	@Override 
	public void onDestroy() {
	  super.onDestroy();

	  _shutdownService();

	}
	private void _startService() {
	  timer.scheduleAtFixedRate(
	      new TimerTask() {
	        public void run() {
	          verificaAtualizacoes();
	        }
	      },
	      0,
	      UPDATE_INTERVAL);
	  Log.i("TESTESERVICE", "Timer started!!!");
	}


	/** dont forget to fire update to the ui listener */
	private void verificaAtualizacoes() {
	  // http post to the service
	  Log.i(getClass().getSimpleName(), "background task - start");

	  List<ObjetoRastreavel> objetos = CorreioController.getInstancia().getOjbetosRastreaveis();
	  for (ObjetoRastreavel objetoRastreavel : objetos) {
		 if(objetoRastreavel.isVerifica()){
//			 if(CorreioController.getInstancia().verificaNovaInformacao(objetoRastreavel, informacoes)){
//				 
//			 }
		 }
	  }
	  
	}

	private void _shutdownService() {
	  if (timer != null) timer.cancel();
	  Log.i(getClass().getSimpleName(), "Timer stopped!!!");
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	}//end class MyService
